package com.qianxun.Test.webServiceTest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class testWeather {
    public static void main(String[] args) throws Exception {
        System.out.println("请输入查询的城市的名称");
        String theCityCode=new Scanner(System.in).nextLine();
        testWeather.get(theCityCode,"");
    }
    public static void get(String theCityCode ,String userID ) throws Exception{
        URL url=new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theCityCode="+theCityCode+
                "&userID="+userID);
        String boas=testWeather.cq(url);
            System.out.println(boas);
        }
        public static String cq(URL url) throws IOException {
        String boa="";
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){ //结果码=200
                InputStream is=conn.getInputStream();
                //内存流 ，
                ByteArrayOutputStream boas=new ByteArrayOutputStream();
                byte[] buffer=new byte[1024];
                int len=-1;
                while((len=is.read(buffer))!=-1){
                    boas.write(buffer, 0, len);
                }
                boas.close();
                is.close();
               boa=boas.toString();
        }
            return boa;
    }

}
